package com.minhua.jijiehao2.mainframe.main.impl

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.support.v4.app.Fragment
import android.widget.RadioButton
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.mainframe.account.accountClassFragment.impl.AccountClassFragment
import com.minhua.jijiehao2.mainframe.account.accountFragment.impl.AccountFragment
import com.minhua.jijiehao2.mainframe.creditcard.creditcardfragment.impl.CreditCardFragment
import com.minhua.jijiehao2.mainframe.home.homeFragment.impl.HomeFragment
import com.minhua.jijiehao2.mainframe.loan.loanfragment.impl.LoanFragment
import com.minhua.jijiehao2.mainframe.main.imp.IMainView
import com.minhua.jijiehao2.mainframe.mine.mineFragment.impl.MineFragment
import com.minhua.jijiehao2.utils.ActivityTaskManager
import com.minhua.jijiehao2.utils.CountUtils
import com.minhua.jijiehao2.utils.SPUtils
import com.minhua.jijiehao2.utils.T
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity<IMainView, MainPresenter>(), IMainView {

    private var currentIndex: Int = 0

    override fun getLayoutId(): Int = R.layout.activity_main

    override fun getPresenter(): MainPresenter = MainPresenter()

    companion object {
        fun intentStart(context: Context, index: Int) {
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra("index", index)
            context.startActivity(intent)
        }
    }


    override fun initView() {
        val fragments = arrayListOf<Fragment>()
        val switch = SPUtils.get(DefineKey.SWITCH, 0)
//        val switch = 1
        when (switch) {
            0 -> {
                fragments.add(AccountFragment.newInstance())
                fragments.add(AccountClassFragment.newInstance())
                fragments.add(MineFragment.newInstance())
                rgMainBottom.removeViewAt(1)
                tvGold.text = getString(R.string.mainAccount)
                rgMainBottom.removeViewAt(rgMainBottom.childCount - 2)
            }
            1 -> {
                fragments.add(AccountFragment.newInstance())
                //圈子暂时替换列表
//                fragments.add(CircleFragment.newInstance())
                fragments.add(LoanFragment.newInstance())
                fragments.add(HomeFragment.newInstance())
                fragments.add(CreditCardFragment.newInstance())
                fragments.add(MineFragment.newInstance())
            }
        }

        rgMainBottom.setOnCheckedChangeListener { group, checkedId ->
            for (i in 0 until group.childCount) {
                val fragment = fragments[i]
                if (group.getChildAt(i).id == checkedId) {
//                    if ((i == group.childCount - 2 || i == 1) && !LoginActivity.logged(this)) {
//                        (rgMainBottom.getChildAt(currentIndex) as RadioButton).isChecked = true
//                        return@setOnCheckedChangeListener
//                    }
                    if (fragment.isAdded) {
                        supportFragmentManager.beginTransaction().show(fragment).commit()
                    } else {
                        supportFragmentManager.beginTransaction().add(R.id.flMainContainer, fragment, i.toString()).commit()
                    }
                    currentIndex = i
                    if (fragment is LoanFragment) {
                        CountUtils.recodeDetail(CountUtils.LOANSTABS, "0")
                    }
                    if (fragment is CreditCardFragment) {
                        CountUtils.recodeDetail(CountUtils.CREDITTABS, "0")
                    }
                } else {
                    if (fragment.isAdded && !fragment.isHidden) {
                        supportFragmentManager.beginTransaction().hide(fragment).commit()
                    }
                }
            }
        }

        if (switch == 1) {
            (rgMainBottom.getChildAt(fragments.size / 2) as RadioButton).isChecked = true
        } else {
            (rgMainBottom.getChildAt(0) as RadioButton).isChecked = true
        }
        tvGold.setOnClickListener { rbMainGold.isChecked = true }
        checkIndex(intent.getIntExtra("index", -1))

    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        checkIndex(intent?.getIntExtra("index", -1) ?: -1)
    }

    override fun initData() {

    }

    private var lastBackTime = 0L
    override fun onBackPressed() {
        val currentTimeMillis = System.currentTimeMillis()
        if (currentTimeMillis - lastBackTime > 2000) {
            T.showShort("再次点击返回退出")
            lastBackTime = currentTimeMillis
        } else {
            ActivityTaskManager.getActivityTaskManager().finishAllActivity()
        }
    }

    private fun checkIndex(index: Int) {
        if (index == -1) {
            return
        }
        (rgMainBottom.getChildAt(index) as RadioButton).isChecked = true
    }

    override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {

    }
}

